using System;
using AndyPike.Unfuddle.Commands.Tickets;
using AndyPike.Unfuddle.Contracts;
using AndyPike.Unfuddle.Entities.Tickets;
using NUnit.Framework;
using Rhino.Mocks;

namespace AndyPike.Unfuddle.Tests.Unit.Commands.Tickets
{
    [TestFixture]
    public class When_deleting_a_ticket
    {
        private MockRepository mockery;
        private IUnfuddleService service;
        private DeleteTicket command;
        private int projectId;
        private int ticketId;

        [SetUp]
        public void SetUp()
        {
            mockery = new MockRepository();
            service = mockery.DynamicMock<IUnfuddleService>();
            projectId = 123;
            ticketId = 456;

            command = new DeleteTicket(projectId, ticketId);
        }

        [Test]
        public void Should_leverage_the_unfuddle_service()
        {
            const string url = "http://www.domain.com";

            using (mockery.Record())
            {
                Expect.Call(service.BuildUrl(null)).IgnoreArguments().Return(url);
                Expect.Call(service.InvokeRequest<Ticket>(url, HttpMethod.DELETE, null)).Return(null);
            }

            using (mockery.Playback())
            {
                command.Execute(service);
            }
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_throw_an_exception_if_a_null_service_is_passed_in()
        {
            command.Execute(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Should_throw_an_exception_if_the_project_id_is_less_than_1()
        {
            new DeleteTicket(0, ticketId);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Should_throw_an_exception_if_the_ticket_id_is_less_than_1()
        {
            new DeleteTicket(projectId, 0);
        }
    }
}